package com.amkai.aes.webservice.binary.objecthandler.prefcards;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.amkai.aes.aescommon.service.binary.job.prefcards.PhysStaffLoaderJob;
import com.amkai.aes.aescommon.service.binary.job.prefcards.PhysStaffLoaderParams;
import com.amkai.aes.aescommon.service.binary.job.prefcards.PhysStaffLoaderResult;
import com.amkai.aes.generatedbeans.physstaff.Physician;
import com.amkai.aes.generatedbeans.preferencecard.SurgeonPreferences;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class PhysStaffLoaderHandler implements JobObjectHandler<PhysStaffLoaderJob, PhysStaffLoaderParams, PhysStaffLoaderResult> {
	private static final Log log = LogFactory.getLog(PhysStaffLoaderHandler.class);

	private final static String QUERY = "SELECT phys FROM Physician phys WHERE phys.id = :physId ";

	@Override
	public PhysStaffLoaderResult process(PhysStaffLoaderJob job,
			PhysStaffLoaderParams object) {
		PhysStaffLoaderResult result = new PhysStaffLoaderResult();
		SessionManager sm = null;
		try {
			sm = SiteManager.Start();
			sm.openSession();

			Query query = sm.execute(QUERY).setLong("physId",
					object.getPhysStaffID());
			Physician resultPhysStaff = (Physician) query.uniqueResult();
			if (resultPhysStaff != null) {

				log.debug("Result physician : " + resultPhysStaff);
				resultPhysStaff.getKind().name();
				for (SurgeonPreferences surgPref : resultPhysStaff
						.getSurgeonPreferences()) {
					surgPref.getId();
					if (surgPref.getAnesthesiologist() != null) {
						surgPref.getAnesthesiologist().getName();
					}
					if (surgPref.getCrna() != null) {
						surgPref.getCrna().getName();
					}
					if (surgPref.getCirculatingNurse() != null) {
						surgPref.getCirculatingNurse().getName();
					}
					if (surgPref.getLastRevisitedBy() != null) {
						surgPref.getLastRevisitedBy().getId();
						surgPref.getLastRevisitedBy().hasChangedProperty();

					}
					if (surgPref.getPreferedLab() != null) {
						surgPref.getPreferedLab().getId();
					}
					if (surgPref.getScrubNurse() != null) {
						surgPref.getScrubNurse().getName();
					}
					if (surgPref.getPhysician() != null) {
						surgPref.getPhysician().getId();
					}
					
				}
				resultPhysStaff.getName();
				if (resultPhysStaff.getSpecialty() != null) {
					resultPhysStaff.getSpecialty().getName();
				}

				resultPhysStaff.getSurgeonPreferences();
				result.setResult(resultPhysStaff);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (sm != null) {
				sm.closeSession();
			}
		}
		return result;
	}

}
